#!/bin/bash
#############################################################
# Created   11 July 2011 - Venki
# Modified  14 January 2012 - Venki
############################################################# 
# The purpose of this script is to transfer all the tar files 
# placed under ~/Scripts/export/ directory in user space to 
# tardis. 
# This script will be called by extractmeta.sh automatically.
# However, ths user can execute this script manually by using 
# the following command to run in the background.
# ./exports.sh &
#############################################################
# EXPORT PARAMETERS
#############################################################
#
PORTNO=8889
#PORTNO=22
TUSERNAME=''
TPASSWORD=''
TARDISDIR=''
EID=''
URL="http://gaia1.isis.rmit.edu.au:8890"
FNAME=''
#
#############################################################
# EXPORTDIR contains all the files that has to be exported
#############################################################
#varibale to hold the base directory
BASEDIR="$HOME/Scripts"
EXPORTDIR="$BASEDIR/exportdir"
LOGFILE="$BASEDIR/log"
PACKAGE=""
#############################################################
#
# For every tar files in the export directory.
# Transfer each files to Tardis one at the time. 
############################################################# 

#for item in `find $EXPORTDIR -type f -maxdepth 1 -mindepth 1 \( -name "*.tar" \)`
rsync_pro(){

  dirname=$1
  subdir=`echo $dirname | awk -F. '{ print $2 }'`
  PACKAGE="$subdir"
  echo $TARDISDIR

  if rsync -rLtoD --no-p  --chmod=Du+rx,g+srx,o=,Fu+rx,g+rx,o=r --rsh="ssh -i $HOME/.ssh/id_hpc -p $PORTNO" $EXPORTDIR/$dirname $TUSERNAME@gaia1.isis.rmit.edu.au:$TARDISDIR/
  then
         log=`curl  -d "eid=$EID&desc=$PACKAGE&folder=$dirname&username=$TUSERNAME&password=$TPASSWORD&authMethod=localdb" $URL/apps/hpctardis/addfiles/ 2> /dev/null`
         echo $log > $LOGFILE/error-detail.html
         echo "`date`	$EXPORTDIR/$dirname  $log added to Tardis" >> $LOGFILE/$PACKAGE.log 
         rm -r $EXPORTDIR/$dirname
  else
         echo "`date`    $EXPORTDIR/$dirname  $log is not added to Tardis" >> $LOGFILE/$PACKAGE.log
  fi

  return

}

#Main Program
#Aunthenticate Tardis Using

if [ -e $BASEDIR/.config ]
then
   TUSERNAME=`cat $BASEDIR/.config | head -1 | tr -d '\n'`
   TPASSWORD=`cat $BASEDIR/.config | tail -1 | tr -d '\n'`
else
   echo "The config doesnt exsist for Tardis " >> $LOGFILE/script.log
fi


for item in `find $EXPORTDIR -maxdepth 1 -mindepth 1 -type d`
do
    dirname=`basename $item`
    metapack=`echo $dirname | awk -F . {'print $2'}`
    login=`curl -F "file=@$EXPORTDIR/$dirname/metadata.$metapack;type=text/plain" -F "username=$TUSERNAME" -F "password=$TPASSWORD" -F "authMethod=localdb" $URL/apps/hpctardis/login/ 2> /dev/null`

    if [[ $login == "Unsuccessful" ]]
    then
       echo "`date` Tardis login was not successful please run execsum.sh again" >> $LOGFILE/fta-script.log
       rm -f $BASEDIR/.config
       #TODO Include exit command to come out
    else
       TMP="$login"
       TARDISDIR=`echo $TMP | awk -F@ '{ print $1 }'`
       EID=`echo $TMP | awk -F@ '{ print $2 }'`
       FNAME=`echo $TMP | awk -F@ '{ print $3 }'`
       echo $FNAME
       echo $dirname

       if [[ $dirname == $FNAME ]]
       then
          rsync_pro $dirname
       else
          echo "`date` The transfer folder $FNAME is corrupted or incorrect metadata file" >> $LOGFILE/fta-script.log
       fi

    fi
done
